package com.ourhealth.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.ourhealth.constant.MessageConstant;
import com.ourhealth.entity.Result;
import com.ourhealth.pojo.OrderSetting;
import com.ourhealth.service.OrderSettingService;
import com.ourhealth.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("orderSetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;
    @RequestMapping("handleOrderSet")
    public Result handleOrderSet(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.handleOrderSet(orderSetting);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
        return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
    }

    @RequestMapping("getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String formatDate){
        List<Map> list = null;
        try {
            list = orderSettingService.getOrderSettingByMonth(formatDate);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("upload")
    public Result upload(@RequestParam("excelFile")MultipartFile excelFile){
        try {
            List<String[]> list = POIUtils.readExcel(excelFile);
            if(list != null && list.size() > 0){
                List<OrderSetting> orderSettingList = new ArrayList<>();
                for (String[] strings : list) {
                        if (strings[0]!=null&&strings[0].trim()!=""){
                            OrderSetting orderSetting =
                                    new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
                            orderSettingList.add(orderSetting);
                        }
                }
                orderSettingService.addOrderSettingList(orderSettingList);
            }
        } catch (IOException e) {
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }
}
